﻿using Fozzy;
using Fozzy.Family;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Microsoft.FamilyShowLib
{
    public class Age                                                            : Integer
    {

        #region States
            public bool                 Be(Age age)                             { return Match(age)                 ; }
        #endregion

        #region Constructors

        public Age(string filter)                                               : base(filter)
            { }

        #endregion

            /// <summary>
            /// The Parse of the Filter into Birth and Death heavily relies interpretation of :
            /// The string conveying a Range of Age
            /// "-" = Birth information
            /// "+" = Death information
            /// </summary>
            protected override void Prepare()
            {
                base.Prepare()                                                  ;
                // Single age.
                Minimise(Parse(Name))                                                                               ;

                // Age range.
                if (Name.Contains("-"))
                {
                    string[] list = Name.ToString().Split('-')                                                      ;
                    Minimise(Parse(list[0]))                                                                        ;
                    Maximise(Parse(list[1]))                                                                        ;
                }

                // Ending age.
                if (Name.ToString().EndsWith("+")) Maximise(Parse(Name.ToString().Substring(0, Name.Length - 1)))   ;
            }

            private bool Match(Age age)
            {
                // Check single age.
                if (Be_Minimized && age.Value == Minimum) return true                           ;  

                // Check for a range.
                if (Be_Ranged && age.In(Minimum,Maximum)) return true                           ;

                // Check for an ending age.
                if (!Be_Minimized && Be_Maximized && age.Value >= Maximum) return true          ;

                return false                                                                    ;
            }




        // Living, example: 1900 | 107
            //    if (person.IsLiving)
            //    {
            //        if (person.BirthDate == null)
            //            return string.Empty;

            //        if (!person.Age.HasValue)
            //            return string.Empty;
                        
            //        int age = person.Age.Value - (DateTime.Now.Year - (int)this.displayYear);
            //        return string.Format(CultureInfo.CurrentUICulture,
            //            "{0} | {1}", person.BirthDate.Value.Year, Math.Max(0, age));
            //    }
                
            //    // Deceased, example: 1900 - 1950 | 50                    
            //    if (person.BirthDate != null && person.DeathDate != null)
            //    {
            //        if (!person.Age.HasValue)
            //            return string.Empty;
                        
            //        int age = (this.displayYear >= person.DeathDate.Value.Year) ? 
            //            person.Age.Value : person.Age.Value - (person.DeathDate.Value.Year - (int)this.displayYear);
                
            //        return string.Format(CultureInfo.CurrentUICulture, 
            //            "{0} - {1} | {2}", person.BirthDate.Value.Year,
            //            person.DeathDate.Value.Year, Math.Max(0, age));
            //    }

            //    // Deceased, example: ? - 1950 | ?
            //    if (person.BirthDate == null && person.DeathDate != null)
            //    {
            //        return string.Format(CultureInfo.CurrentUICulture, 
            //            "? - {0} | ?", person.DeathDate.Value.Year);
            //    }

            //    // Deceased, example: 1900 - ? | ?
            //    if (person.BirthDate != null && person.DeathDate == null)
            //    {
            //        return string.Format(CultureInfo.CurrentUICulture,
            //            "{0} - ? | ?", person.BirthDate.Value.Year);
            //    }
                
            //    return string.Empty;
            //}


            //public override void Save()
            //{
            //    // store the person's story into temp directory to be packaged into OPC later
            //            if (p.Story != null)
            //            {
            //                //string storyOldPath = Path.Combine(Path.Combine(
            //                //    Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
            //                //    App.ApplicationFolderName), p.Story.RelativePath);
            //                if (File.Exists(storyOldPath))
            //                {
            //                    string storyFile = Path.Combine(storyFolder, Path.GetFileName(p.Story.AbsolutePath));

            //                    // Remove spaces since they'll be packaged as %20, breaking relative paths that expect spaces
            //                    storyFile = ReplaceEncodedCharacters(storyFile);
            //                    p.Story.RelativePath = ReplaceEncodedCharacters(p.Story.RelativePath);

            //                    File.Copy(storyOldPath, storyFile, true);
            //                }
            //            }
            //        }
            //}

}}
